package com.liran.wx.config;

import com.liran.wx.handler.*;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType.EVENT;

/**
 * @author: whh
 * @Date: 2022/7/9
 * @Description:
 */
@Configuration
@AllArgsConstructor
public class WxJavaConfig {

    private final WxMpService wxMpService;

    private final WxMessageRedisDuplicateChecker wxMessageRedisDuplicateChecker;

    private final TextHandler textHandler;

    private final ImageHandler imageHandler;

    private final SubscribeHandler subscribeHandler;

    private final UnSubscribeHandler unSubscribeHandler;

    private final MenuHandler menuHandler;


    @Bean
    public WxMpMessageRouter wxMpMessageRouter() {
        // 创建消息路由
        final WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
        //设置自定义的基于redis的消息排重器
        router.setMessageDuplicateChecker(wxMessageRedisDuplicateChecker);
        // 自定义菜单事件
        router.rule().async(false).msgType(EVENT).event(WxConsts.EventType.CLICK).handler(menuHandler).end();
        // 添加文本消息路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.TEXT).handler(textHandler).end();
        // 添加图片消息路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.IMAGE).handler(imageHandler).end();
        //添加关注事件推送路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler).end();
        //添加取消关注事件推送路由
        router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT).event(WxConsts.EventType.UNSUBSCRIBE).handler(unSubscribeHandler).end();
        return router;
    }
}
